<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">

		<title>RPC-LIST</title>
		<meta name="keywords" content="RPC-LIST">
		<meta name="description" content="RPC-LIST">

		<link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
		<link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">
		<link href="../css/animate.css" rel="stylesheet">
		<link href="../css/style.css?v=4.1.0" rel="stylesheet">

		<!-- 表格 -->
		<link href="../css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
	</head>

	<body class="gray-bg" onload="
				$('#end').val((new Date()).format('yyyy-MM-dd hh:mm:ss'));">
		<div class="wrapper wrapper-content animated fadeInRight">
			<form class="form-horizontal m-t">
				<div class="form-group">
					<div class="col-sm-3">
						<select id="app_chosen" class="form-control m-b" style="width:240px;">
						</select>
					</div>
					<div class="col-sm-3">
						<input class="form-control layer-date" id="start" placeholder="开始日期" onclick="timeShow()">
					</div>
					<div class="col-sm-3">
						<input class="form-control layer-date" id="end" placeholder="结束日期" onclick="timeShow()">
					</div>
					<div class="col-sm-2">
						<button type="button" class="btn btn-primary btn-sm" onclick="tableShow()">搜索</button>
					</div>
				</div>
			</form>
			<div class="row">
				<div class="col-sm-12">
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5><i class="fa fa-table"></i> 服务耗时散点图</h5>
							<div class="ibox-tools">
								<a class="collapse-link">
									<i class="fa fa-chevron-up"></i>
								</a>
								<a class="dropdown-toggle" data-toggle="dropdown" href="rpc_list.html#">
									<i class="fa fa-wrench"></i>
								</a>
								<ul class="dropdown-menu dropdown-user">
									<li>
										<a href="rpc_list.html#">选项1</a>
									</li>
									<li>
										<a href="rpc_list.html#">选项2</a>
									</li>
								</ul>

							</div>
						</div>
						<div class="ibox-content">
							<div class="echarts" id="rpc_scatter_chart"></div>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-sm-12">
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5><i class="fa fa-table"></i> 服务请求列表</h5>
							<div class="ibox-tools">
								<a class="collapse-link">
									<i class="fa fa-chevron-up"></i>
								</a>
								<a class="dropdown-toggle" data-toggle="dropdown" href="rpc_list.html#">
									<i class="fa fa-wrench"></i>
								</a>
								<ul class="dropdown-menu dropdown-user">
									<li>
										<a href="rpc_list.html#">选项1</a>
									</li>
									<li>
										<a href="rpc_list.html#">选项2</a>
									</li>
								</ul>

							</div>
						</div>
						<div class="ibox-content">
							<table id="rpc_tb" class="table table-striped table-bordered table-hover dataTables-example">
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>

		<!-- 全局js -->
		<script src="../js/jquery.min.js?v=2.1.4"></script>
		<script src="../js/bootstrap.min.js?v=3.3.6"></script>

		<!-- 表格 -->
		<script src="../js/plugins/dataTables/jquery.dataTables.js"></script>
		<script src="../js/plugins/dataTables/dataTables.bootstrap.js"></script>

		<!-- 自定义js -->
		<script src="../js/content.js?v=1.0.0"></script>

		<!-- 时间框 -->
		<script src="../js/plugins/layer/laydate/laydate.js"></script>
		<script src="../js/mine/time.js"></script>

		<!-- ECharts -->
		<script src="../js/plugins/echarts/echarts-all.js"></script>
		<!--<script src="../js/demo/echarts-demo.js"></script>-->

		<!-- Page-Level Scripts -->
		<script>
			var rpcListFlag = false;

			$(document).ready(function() {
				appChosen();
			});

			function appChosen() {
				$.get("http://localhost:8041/my/applications", {},
					function(rs) {
						$("#app_chosen").append($("<option>请选择应用...</option>"));
						var data_selector = JSON.parse(rs);
						$.each(data_selector, function(i, item) {
							$("#app_chosen").append($("<option>" + data_selector[i].applicationName + "</option>"));
						});
					});
			}

			function tableShow() {
				var startDate = new Date($("#start").val());
				var endDate = new Date($("#end").val());
				$.get("http://localhost:8041/my/transactionListForConditions", {
						applicationName: $("#app_chosen").val(),
						startTimestamp: Date.parse(startDate),
						endTimestamp: Date.parse(endDate)
					},
					function(rs) {

						var data = JSON.parse(rs);

						var rpc_sc = echarts.init(document.getElementById("rpc_scatter_chart"));
						var rpc_sc_serie = {
							type: 'scatter',
							tooltip: {
								trigger: 'item',
								formatter: function(params) {
									if(params.value.length > 1) {
										return params.seriesName + ' : ' +
											params.value[1] + 'ms<br/>' +
											params.value[0] + '';
									} else {
										return params.seriesName + ' - ' +
											params.name + ' : ' +
											params.value + 'ms';
									}
								}
							},
							markPoint: {
								data: [{
										type: 'max',
										name: '最大耗时'
									},
									{
										type: 'min',
										name: '最小耗时'
									}
								]
							},
							data: [],
							markLine: {
								data: [{
									type: 'average',
									name: '平均耗时'
								}]
							}
						};
						var rpc_so = {
							title: {
								//								subtext: '   desc'
							},
							tooltip: {
								trigger: 'axis',
								showDelay: 2,
								axisPointer: {
									type: 'cross',
									lineStyle: {
										type: 'dashed',
										width: 1
									}
								}
							},
							legend: {
								data: []
							},
							grid: {
								x: 55,
								x2: 30,
								y2: 24
							},
							xAxis: [{
								type: 'time',
								min: startDate,
								max: endDate
							}],
							yAxis: [{
								type: 'value',
								name: 'ms',
								axisLabel: {
									formatter: '{value}'
								}
							}],
							series: []
						};
						rpc_sc_serie.name = "mapph8091";
						var agentIds = new Array();
						$.each(data, function(i, item) {
							rpc_sc_serie.data[i] = [new Date(parseInt(data[i].startTime)), data[i].elapsed];
							agentIds.push(data[i].agentId);
						});
						rpc_so.legend.data = $.unique(agentIds);
						rpc_so.series[0] = rpc_sc_serie;
						rpc_so.title.text = "服务 [ " + $("#app_chosen").val() + " ] 请求耗时";
						rpc_sc.setOption(rpc_so);
						$(window).resize(rpc_sc.resize);

						if(rpcListFlag == true) {
							$("#rpc_tb").dataTable().fnDestroy();
						}

						$("#rpc_tb").empty();
						$("#rpc_tb").append($("<thead><tr><th>序号</th><th>开始时间</th><th>RPC请求</th><th>请求耗时</th><th>实例名</th><th>客户端IP</th><th>事务唯一标识</th></tr></thead><tbody>"));
						$.each(data, function(i, item) {
							var startTime = getFormatDate(data[i].startTime);
							td = $("<tr><td>" + (i + 1) +
								"<td><i class='fa fa-clock-o'></i> " + getFormatDate(data[i].startTime) +
								"<td><a href=\"trans_info.html?traceId=" + data[i].transactionId.agentId + '^' + data[i].transactionId.agentStartTime + '^' + data[i].transactionId.transactionSequence + "&agentId=" + data[i].agentId + "&spanId=" + data[i].spanId + "&mRpc=" + data[i].rpc + "\">" + data[i].rpc +
								"</a></td><td class='text-navy'>" + data[i].elapsed +
								"ms</td><td>" + data[i].agentId +
								"</td><td>" + data[i].remoteAddr +
								"</td><td>" + data[i].transactionId.agentId + '^' + data[i].transactionId.agentStartTime + '^' + data[i].transactionId.transactionSequence + "</td></tr>");
							$("#rpc_tb").append(td);
						});
						$("#rpc_tb").append("</tbody>");
						$('#rpc_tb').dataTable();
						rpcListFlag = true;
					});
			}
		</script>

	</body>

</html>